// pages/add_donation_item/index.js
var router = require('../../utils/router/index.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    donationtext:'点击选择物资类别',
    donationlist:['测试物资1','测试物资2','测试物资源3']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const data = router.extract(options);
    console.log(""+data.donate_id+"    "+data.hospital_id); 
    this.setData({
      donate_id:data.donate_id,
      donate_name:data.donate_name,
      hospital_id: data.hospital_id,
      hospital_name:data.hospital_name
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 物资类别选择
   */
  binddonationChange: function(e){
    var that = this
    this.setData({
      index: e.detail.value,
      donationtext:'',
      goods_name:that.data.donationlist[e.detail.value]
    })
  },

  /**
   * 质量标准
   */
  bindstandInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      standard: e.detail.value
    })
  },

  bindnumsInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      amount: e.detail.value
    })
  },
  bindcInput: function(e){
    var that = this
    console.log(e.detail.value)
    this.setData({
      supplier: e.detail.value
    })
  },



  /**
   * 新增物资按钮
   */
  save_add_donation: function(){
    var that = this
    // if(that.data.goods_name == '' || that.data.standard == '' || that.data.amount  == '' || that.data.supplier == ''){
    //     return
    // }

    const s = new Set();
    s.add({
      "goods_name": "医用防护口罩111",
      "standard": "GB 19083-2010",
      "amount": 1000,
      "supplier": "医疗机构"
    }).add({
      "goods_name": "医用防护口罩2222",
      "standard": "GB 19083-2010",
      "amount": 1000,
      "supplier": "医疗机构"
    })

    console.log(s)

    // for (let key of s.values()) 
    // console.log(key)

    wx.cloud.callFunction({
      // 云函数名称
      name: 'donate',
      // 传给云函数的参数
      // data:{
      //   "action": "add_donated_materials",
      //   "donate_id": that.data.donate_id,
      //   "donate_name": that.data.donate_name,
      //   "donate_hospital_id": that.data.donate_hospital_id,
      //   "donate_hospital_name": that.data.donate_hospital_name,
      //   "materials": [{
      //     "goods_name": that.data.goods_name,
      //     "standard": that.data.standard,
      //     "amount": that.data.amount,
      //     "supplier": that.data.supplier
      //   }],
      //   "logistics": {
      //     "logistics_order": "2761537168",
      //     "logistics_company": "顺丰"
      //   },
      //   "content": "紧急捐赠N95口罩1000个"
      // }
      data:{
        "action": "add_donated_materials",
        "donate_id": "1acf1de95e3bac140b0cd9a9459dee11",
        "donate_name": "yuhi",
        "donate_hospital_id": "d68532785e3abde00ac306ee4178e9e3",
        "donate_hospital_name": "武汉协和医院",
        "materials":Array.from(s),
        "logistics": {
          "logistics_order": "2761537168",
          "logistics_company": "顺丰"
        },
        "content": "紧急捐赠N95口罩1000个"
      }
      
    })
    .then(res => {
      console.log(res.result) 
      // if(res.result.errMsg  == 'collection.add:ok'){
      //   // router.pop()
      // }
    })
    .catch(console.error);
  }

})